package com.yycx.module.bus.provider.controller.hy;

import cn.hutool.core.map.MapUtil;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.security.OpenUser;
import com.yycx.common.utils.ApiAssert;
import com.yycx.module.bus.provider.service.hy.HyErpSearchService;
import com.yycx.module.bus.provider.service.hy.HyRemoteOrderService;
import com.yycx.module.system.client.entity.SysCompany;
import com.yycx.module.system.provider.service.SysCompanyService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 海鹰综合查询表控制器
 *
 * @author flyme
 * @date 2020-11-28
 */
@RestController
@RequestMapping("/hy/ept/search/")
public class HyErpSearchController {

    @Autowired
    private HyErpSearchService hyErpSearchService;

    @Autowired
    private HyRemoteOrderService hyRemoteOrderService;

    @Autowired
    private SysCompanyService companyService;


    @ApiOperation(value = "采购单主", notes = "采购单主")
    @GetMapping(value = "procureList")
    public ResultBody procureList(@RequestParam(required = false) Map params) {
        String dbName = getDbName();
        return hyErpSearchService.procureList(dbName, params, true);
    }


    @ApiOperation(value = "采购单明细", notes = "采购单明细")
    @GetMapping(value = "procureDetailList")
    public ResultBody queryHyOrderDetailList(@RequestParam(required = false) Map params) {
        String dbName = getDbName();
        return hyErpSearchService.procureDetailList(dbName, params, true);
    }


    @ApiOperation(value = "销售单主", notes = "销售单主")
    @GetMapping(value = "saleList")
    public ResultBody saleList(@RequestParam(required = false) Map params) {
        String dbName = getDbName();
        return hyErpSearchService.saleList(dbName, params, true);
    }


    @ApiOperation(value = "销售单明细", notes = "销售单明细")
    @GetMapping(value = "saleDetailList")
    public ResultBody saleDetailList(@RequestParam(required = false) Map params) {
        String dbName = getDbName();
        return hyErpSearchService.saleDetailList(dbName, params, true);
    }

    @ApiOperation(value = "明细账", notes = "明细账")
    @GetMapping(value = "mxzList")
    public ResultBody mxzList(@RequestParam(required = false) Map params) {
        String dbName = getDbName();
        return hyErpSearchService.mxzList(dbName, params, true);
    }


    @ApiOperation(value = "查询部门", notes = "查询部门")
    @GetMapping(value = "selectHyDeptList")
    public ResultBody selectHyDeptList(@RequestParam(required = false) Map params) {
        String dbName = getDbName();
        List<EntityMap> list = hyErpSearchService.selectHyDeptList(dbName, params);
        return ResultBody.ok(list);
    }


    @ApiOperation(value = "查询业务员", notes = "查询业务员")
    @GetMapping(value = "selectHySaleManList")
    public ResultBody selectHySaleManList(@RequestParam(required = false) Map params) {
        String dbName = getDbName();
        List<EntityMap> list = hyErpSearchService.selectHySaleManList(dbName, params);
        return ResultBody.ok(list);
    }

    @ApiOperation(value = "查询客商资料", notes = "查询客商资料")
    @GetMapping(value = "selectHyCustomList")
    public ResultBody selectHyCustomList(@RequestParam(required = false) Map params) {
        String dbName = getDbName();
        String customName = MapUtil.getStr(params, "keyword", "");
        List<EntityMap> list = new ArrayList<>();
        if (FlymeUtils.isNotEmpty(customName)) {
            params.put("customName", customName);
            list = hyErpSearchService.selectHyCustomList(dbName, params);
        }
        return ResultBody.ok(list);
    }

    @ApiOperation(value = "查询商品", notes = "查询商品")
    @GetMapping(value = "selectDrugList")
    public ResultBody selectDrugList(@RequestParam(required = false) Map params) {
        String dbName = getDbName();
        String customName = MapUtil.getStr(params, "keyword", "");
        List<EntityMap> list = new ArrayList<>();
        if (FlymeUtils.isNotEmpty(customName)) {
            params.put("drugsName", customName);
            list = hyErpSearchService.selectDrugList(dbName, params);
        }
        return ResultBody.ok(list);
    }

    @ApiOperation(value = "查询销售历史", notes = "查询销售历史")
    @GetMapping(value = "selectSaleHistoryList")
    public ResultBody selectSaleHistoryList(@RequestParam(required = false) Map params) {
        String dbName = getDbName();
        List<EntityMap> list = hyErpSearchService.selectSaleHistoryList(dbName, params);
        return ResultBody.ok(list);
    }


    @ApiOperation(value = "查询进货历史", notes = "查询进货历史")
    @GetMapping(value = "selectStockList")
    public ResultBody selectStockList(@RequestParam(required = false) Map params) {
        String dbName = getDbName();
        List<EntityMap> list = hyErpSearchService.selectStockList(dbName, params);
        return ResultBody.ok(list);
    }


    @PostMapping(value = "exportProcureList")
    public void exportProcureList(@RequestParam(required = false) Map params, HttpServletRequest request, HttpServletResponse response) {
        params.put("dbName", getDbName());
        params.put("exportTag", "1");
        companyService.export(params, request, response, "采购单据表", "采购单据表", "exportListHandler");
    }


    @PostMapping(value = "exportProcureDetailList")
    public void exportProcureDetailList(@RequestParam(required = false) Map params, HttpServletRequest request, HttpServletResponse response) {
        params.put("dbName", getDbName());
        params.put("exportTag", "2");
        companyService.export(params, request, response, "采购明细表", "采购明细表", "exportListHandler");
    }

    @PostMapping(value = "exportSaleList")
    public void exportSaleList(@RequestParam(required = false) Map params, HttpServletRequest request, HttpServletResponse response) {
        params.put("dbName", getDbName());
        params.put("exportTag", "3");
        companyService.export(params, request, response, "销售订单表", "销售订单表", "exportListHandler");
    }

    @PostMapping(value = "exportSaleDetailList")
    public void exportSaleDetailList(@RequestParam(required = false) Map params, HttpServletRequest request, HttpServletResponse response) {
        params.put("dbName", getDbName());
        params.put("exportTag", "4");
        companyService.export(params, request, response, "销售明细表", "销售明细表", "exportListHandler");
    }

    @PostMapping(value = "exportMxzList")
    public void exportMxzList(@RequestParam(required = false) Map params, HttpServletRequest request, HttpServletResponse response) {
        params.put("dbName", getDbName());
        params.put("exportTag", "5");
        companyService.export(params, request, response, "库存明细账", "库存明细账", "exportListHandler");
    }


    @ApiOperation(value = "更新自定义成本价", notes = "更新自定义成本价")
    @PostMapping(value = "updateCostPrice")
    public ResultBody updateCostPrice() {
        String dbName = getDbName();
        hyRemoteOrderService.updateCostPrice(dbName);
        return ResultBody.msg("更新成功");
    }


    private String getDbName() {
        OpenUser openUser = OpenHelper.getUser();
        Long companyId = openUser.getCompanyId();
        SysCompany company = companyService.getById(companyId);
        String dbName = "";
        if (FlymeUtils.isNotEmpty(company)) {
            dbName = company.getBusinessScope();
            ApiAssert.isNotEmpty("账套实例未配置", dbName);
        }
        return dbName;
    }
}
